home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Net / hostent.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  4.0 KB  |  149 lines

  1. package Net::hostent;
  2. use strict;
  3.  
  4. BEGIN { 
  5.     use Exporter   ();
  6.     use vars       qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  7.     @EXPORT      = qw(gethostbyname gethostbyaddr gethost);
  8.     @EXPORT_OK   = qw(
  9.             $h_name            @h_aliases
  10.             $h_addrtype     $h_length
  11.             @h_addr_list     $h_addr
  12.            );
  13.     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  14. }
  15. use vars      @EXPORT_OK;
  16.  
  17. sub import { goto &Exporter::import }
  18.  
  19. use Class::Struct qw(struct);
  20. struct 'Net::hostent' => [
  21.    name        => '$',
  22.    aliases    => '@',
  23.    addrtype    => '$',
  24.    'length'    => '$',
  25.    addr_list    => '@',
  26. ];
  27.  
  28. sub addr { shift->addr_list->[0] }
  29.  
  30. sub populate (@) {
  31.     return unless @_;
  32.     my $hob = new();
  33.     $h_name      =    $hob->[0]              = $_[0];
  34.     @h_aliases     = @{ $hob->[1] } = split ' ', $_[1];
  35.     $h_addrtype  =    $hob->[2]          = $_[2];
  36.     $h_length     =    $hob->[3]          = $_[3];
  37.     $h_addr      =                             $_[4];
  38.     @h_addr_list = @{ $hob->[4] } =          @_[ (4 .. $#_) ];
  39.     return $hob;
  40.  
  41. sub gethostbyname ($)  { populate(CORE::gethostbyname(shift)) } 
  42.  
  43. sub gethostbyaddr ($;$) { 
  44.     my ($addr, $addrtype);
  45.     $addr = shift;
  46.     require Socket unless @_;
  47.     $addrtype = @_ ? shift : Socket::AF_INET();
  48.     populate(CORE::gethostbyaddr($addr, $addrtype)) 
  49.  
  50. sub gethost($) {
  51.     if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
  52.     require Socket;
  53.     &gethostbyaddr(Socket::inet_aton(shift));
  54.     } else {
  55.     &gethostbyname;
  56.     } 
  57.  
  58. 1;
  59. __END__
  60.  
  61. =head1 NAME
  62.  
  63. Net::hostent - by-name interface to Perl's built-in gethost*() functions
  64.  
  65. =head1 SYNOPSIS
  66.  
  67.  use Net::hostnet;
  68.  
  69. =head1 DESCRIPTION
  70.  
  71. This module's default exports override the core gethostbyname() and
  72. gethostbyaddr() functions, replacing them with versions that return
  73. "Net::hostent" objects.  This object has methods that return the similarly
  74. named structure field name from the C's hostent structure from F<netdb.h>;
  75. namely name, aliases, addrtype, length, and addr_list.  The aliases and
  76. addr_list methods return array reference, the rest scalars.  The addr
  77. method is equivalent to the zeroth element in the addr_list array
  78. reference.
  79.  
  80. You may also import all the structure fields directly into your namespace
  81. as regular variables using the :FIELDS import tag.  (Note that this still
  82. overrides your core functions.)  Access these fields as variables named
  83. with a preceding C<h_>.  Thus, C<$host_obj-E<gt>name()> corresponds to
  84. $h_name if you import the fields.  Array references are available as
  85. regular array variables, so for example C<@{ $host_obj-E<gt>aliases()
  86. }> would be simply @h_aliases.
  87.  
  88. The gethost() funtion is a simple front-end that forwards a numeric
  89. argument to gethostbyaddr() by way of Socket::inet_aton, and the rest
  90. to gethostbyname().
  91.  
  92. To access this functionality without the core overrides,
  93. pass the C<use> an empty import list, and then access
  94. function functions with their full qualified names.
  95. On the other hand, the built-ins are still available
  96. via the C<CORE::> pseudo-package.
  97.  
  98. =head1 EXAMPLES
  99.  
  100.  use Net::hostent;
  101.  use Socket;
  102.  
  103.  @ARGV = ('netscape.com') unless @ARGV;
  104.  
  105.  for $host ( @ARGV ) {
  106.  
  107.     unless ($h = gethost($host)) {
  108.     warn "$0: no such host: $host\n";
  109.     next;
  110.     }
  111.  
  112.     printf "\n%s is %s%s\n", 
  113.         $host, 
  114.         lc($h->name) eq lc($host) ? "" : "*really* ",
  115.         $h->name;
  116.  
  117.     print "\taliases are ", join(", ", @{$h->aliases}), "\n"
  118.         if @{$h->aliases};     
  119.  
  120.     if ( @{$h->addr_list} > 1 ) { 
  121.     my $i;
  122.     for $addr ( @{$h->addr_list} ) {
  123.         printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
  124.     } 
  125.     } else {
  126.     printf "\taddress is [%s]\n", inet_ntoa($h->addr);
  127.     } 
  128.  
  129.     if ($h = gethostbyaddr($h->addr)) {
  130.     if (lc($h->name) ne lc($host)) {
  131.         printf "\tThat addr reverses to host %s!\n", $h->name;
  132.         $host = $h->name;
  133.         redo;
  134.     } 
  135.     }
  136.  }
  137.  
  138. =head1 NOTE
  139.  
  140. While this class is currently implemented using the Class::Struct
  141. module to build a struct-like class, you shouldn't rely upon this.
  142.  
  143. =head1 AUTHOR
  144.  
  145. Tom Christiansen
  146.